package com.yangxk.kadmin.common.util;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class BeanUtil {
    private BeanUtil() {}

    /**
     * map转对象
     * create by yangxk on 2018/9/12 14:49
     */
    public static <T> T map2Bean(Map<String, ?> map, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            org.apache.commons.beanutils.BeanUtils.populate(t, map);

            return t;
        } catch (Exception e) {
            throw new RuntimeException("map2bean exception:" + e.getMessage());
        }
    }

    /**
     * 对象转map
     * create by yangxk on 2018/9/12 14:49
     */
    public static <T> Map<?, ?> bean2Map(T bean) {
        if (bean == null)
            return null;

        return new org.apache.commons.beanutils.BeanMap(bean);
    }

}
